//
//  MLoginViewController.m
//  NeusoftHBSJSober
//
//  Created by Apple-21 on 15/11/3.
//  Copyright (c) 2015年 com.neusoftsober. All rights reserved.
//

#import "MLoginViewController.h"
#import <UIKit/UIKit.h>
#import "MViewUtil.h"
#import "MRegistService.h"
#import "MRegisterViewController.h"
#import "MDDataBaseUtil.h"
#import "AppDelegate.h"
@interface MLoginViewController ()<UITextFieldDelegate> {
    UITextField *accountTxt;
    UITextField *pwdTxt;
    UIButton *showPwdBtn;
    UIButton *loginBtn;
    MRegisterViewController *mRegisterVC;
}
@property (weak, nonatomic) IBOutlet UIImageView *myImageView;
- (IBAction)backBtnAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIView *accountOrPwdView;

@end

@implementation MLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBackgroundEffect];
    
    [self setAccountOrPwdViewStyle];
    [loginBtn setEnabled:NO];
}

- (void)viewWillAppear:(BOOL)animated {
    accountTxt.text = [MDDataBaseUtil userLoginName];
}
#pragma mark 设置背景模糊
- (void)setBackgroundEffect {

    UIVisualEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    
    visualEffectView.frame = [[UIScreen mainScreen] bounds];
    
    [self.myImageView addSubview:visualEffectView];
}
- (void)setAccountOrPwdViewStyle {

    accountTxt = [[UITextField alloc]initWithFrame:CGRectMake(SCREENWIDTH/5/2, 0, SCREENWIDTH*4/5, 44)];
    [accountTxt.layer setBorderWidth:1.0];
    [accountTxt.layer setBorderColor:[UIColor whiteColor].CGColor];
    [accountTxt.layer setMasksToBounds:YES];
    [accountTxt.layer setCornerRadius:20.0];
    [accountTxt setPlaceholder:@"用户名/手机号/邮箱"];
    [accountTxt setTextAlignment:NSTextAlignmentCenter];
    [accountTxt setTextColor:[UIColor lightGrayColor]];
    [accountTxt setFont:[UIFont fontWithName:nil size:18.0]];
    [accountTxt setTintColor:[UIColor lightGrayColor]];
    [self.accountOrPwdView addSubview:accountTxt];
    [accountTxt setClearButtonMode:UITextFieldViewModeWhileEditing];
    accountTxt.delegate = self;
    [accountTxt setTag:0];
    
    pwdTxt = [[UITextField alloc]initWithFrame:CGRectMake(SCREENWIDTH/5/2, 44+15, SCREENWIDTH*4/5, 44)];
    [pwdTxt.layer setBorderWidth:1.0];
    [pwdTxt.layer setBorderColor:[UIColor whiteColor].CGColor];
    [pwdTxt.layer setMasksToBounds:YES];
    [pwdTxt.layer setCornerRadius:20.0];
    [pwdTxt setPlaceholder:@"密码"];
    [pwdTxt setTextAlignment:NSTextAlignmentCenter];
    [pwdTxt setTextColor:[UIColor lightGrayColor]];
    [pwdTxt setFont:[UIFont fontWithName:nil size:18.0]];
    [pwdTxt setTintColor:[UIColor lightGrayColor]];
    [pwdTxt setSecureTextEntry:YES];
    [self.accountOrPwdView addSubview:pwdTxt];
    pwdTxt.delegate = self;
    [pwdTxt setTag:1];
    
    
    showPwdBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
    [showPwdBtn setBackgroundImage:[UIImage imageNamed:@"ic_password_hidden"] forState:UIControlStateNormal];
    [showPwdBtn setBackgroundImage:[UIImage imageNamed:@"ic_password_visible"] forState:UIControlStateHighlighted];
    [showPwdBtn setBackgroundImage:[UIImage imageNamed:@"ic_password_visible"] forState:UIControlStateSelected];
    [showPwdBtn addTarget:self action:@selector(showPasswordAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:showPwdBtn];
    UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(pwdTxt.bounds.size.width-10, self.accountOrPwdView.frame.origin.y+59, 44, 44)];
    [rightView addSubview:showPwdBtn];
    [self.view addSubview:rightView];
    [self.view bringSubviewToFront:rightView];
    
    loginBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREENWIDTH/5/2, 88+30, SCREENWIDTH*4/5, 44)];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setBackgroundColor:BULEHE];
    [loginBtn.layer setMasksToBounds:YES];
    [loginBtn.layer setCornerRadius:20.0];
    [loginBtn.titleLabel setFont:[UIFont fontWithName:nil size:18.0]];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    [loginBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
    [loginBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [loginBtn addTarget:self action:@selector(loginBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.accountOrPwdView addSubview:loginBtn];
    

    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(SCREENWIDTH/2, 132+45, 1, 30)];
    [lineView.layer setBackgroundColor:[UIColor whiteColor].CGColor];
    [self.accountOrPwdView addSubview:lineView];
    
    UIButton *forgetPwdBtn = [[UIButton alloc]initWithFrame:CGRectMake(lineView.frame.origin.x-130,132+45, 90, 30)];
    [forgetPwdBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [forgetPwdBtn.titleLabel setFont:[UIFont fontWithName:nil size:15.0]];
    [forgetPwdBtn.titleLabel setTextAlignment:NSTextAlignmentRight];
    [forgetPwdBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [forgetPwdBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    [forgetPwdBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
    [self.accountOrPwdView addSubview:forgetPwdBtn];
    
    UIButton *registerBtn = [[UIButton alloc]initWithFrame:CGRectMake(lineView.frame.origin.x+40,132+45, 90, 30)];
    [registerBtn setTitle:@"立即注册" forState:UIControlStateNormal];
    [registerBtn.titleLabel setFont:[UIFont fontWithName:nil size:15.0]];
    [registerBtn.titleLabel setTextAlignment:NSTextAlignmentLeft];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registerBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    [registerBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
    [self.accountOrPwdView addSubview:registerBtn];
    
    [registerBtn addTarget:self action:@selector(goRegisterAction:) forControlEvents:UIControlEventTouchUpInside];

    
}
#pragma mark 点击👀 显示密码
- (void)showPasswordAction:(id)sender {

    if (showPwdBtn.selected == YES) {
        
        showPwdBtn.selected = NO;
        [pwdTxt setSecureTextEntry:YES];
    } else {
        showPwdBtn.selected = YES;
        [pwdTxt setSecureTextEntry:NO];
    }
}
#pragma mark  登录按钮
- (void)loginBtnAction:(id)sender {
    
    if ([accountTxt.text isEqualToString:@""]) {
        [[MViewUtil instance] showShortDurationMessage:@"! 请输入账号" targetView:self.view];
    } else {
    
        if ([pwdTxt.text isEqualToString:@""]) {
            [[MViewUtil instance]showShortDurationMessage:@"! 请输入密码" targetView:self.view];
        } else {
        
            MViewUtil *viewUtil = [[MViewUtil alloc]init];
            [viewUtil showLoadingText:@"正在登录..." targetView:self.view];
            [[MRegistService sharedClient] getLoginWithUsername:accountTxt.text password:pwdTxt.text completion:^(MUserModel *model){
                
                // 将登陆账号和密码保存在本地
                [MDDataBaseUtil setUserLoginName:model.loginName];
                [MDDataBaseUtil setUserPassword:model.loginPwd];
                [MDDataBaseUtil setUserMoney:model.money];
                [MDDataBaseUtil setUserID:model.ID];
                AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
                appDelegate.isLoginIndex = 1;
                [viewUtil hideLoadingTextFromTargetView:self.view];
                [self dismissViewControllerAnimated:YES completion:nil];
                
            } failure:^(NSString *error){
                
                [viewUtil showShortDurationMessage:error targetView:self.view];
            }];
        }
    }
    

}

- (void)goRegisterAction:(id)sender {

    mRegisterVC = [[MRegisterViewController alloc]init];
    mRegisterVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:mRegisterVC animated:YES completion:nil];
}
#pragma mark 关闭键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [accountTxt resignFirstResponder];
    [pwdTxt resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [accountTxt resignFirstResponder];
    [pwdTxt resignFirstResponder];
    return YES;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    if (textField.tag == 0) {
        if (![pwdTxt.text isEqualToString:@""]) {
            [loginBtn setEnabled:YES];
            
        }
    } else {
    
        if (![accountTxt.text isEqualToString:@""]) {
            [loginBtn setEnabled:YES];
        }
    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)backBtnAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
